Java JavaScript Python C# C C++ Go Kotlin PHP Swift R Ruby TypeScript Scala SQL Perl rust VisualBasic Matlab Julia

Introduction

Loops in java

What is loops?

Loops are used to execute a block of code repeatedly until a certain condition is met. There are three types of loops in Java: For loop: The for loop is used to execute a block of code a specified number of times. While loop: The while loop is used to execute a block of code as long as a certain condition is true. Do-while loop: The do-while loop is similar to the while loop, but the block of code is executed at least once, even if the condition is false.

For loop - syntax

For loop - syntax
for (initialization; condition; increment) { // block of code }
The initialization statement is executed once before the loop starts. The condition statement is evaluated before each iteration of the loop. If the condition is true, then the block of code is executed. The increment statement is executed after each iteration of the loop.

While loop - syntax

While loop- syntax
while (condition) { // block of code }
The condition statement is evaluated before each iteration of the loop. If the condition is true, then the block of code is executed. The loop will continue to iterate until the condition is false.

do while loop - syntax

do While loop- syntax
do { // block of code } while (condition);
The block of code is executed first, and then the condition statement is evaluated. If the condition is true, then the loop will iterate again. The loop will continue to iterate until the condition is false. Here is an example of a for loop:
For-loop example
public class Main{ public static void main(String[] args){ for (int i = 0; i < 10; i++) { System.out.println(i); } } }

Output

0 1 2 3 4 5 6 7 8 9
In this example, the for loop will print the numbers from 0 to 9. Here is an example of a while loop:
While loop example
public class Main{ public static void main(String[] args){ int i = 0; while (i < 10) { System.out.println(i); i++; } } }

Output

0 1 2 3 4 5 6 7 8 9
In this example, the while loop will print the numbers from 0 to 9. Here is an example of a do-while loop:
do while example
public class Main{ public static void main(String[] args){ int i = 0; do { System.out.println(i); i++; } while (i < 10); } }

Output

0 1 2 3 4 5 6 7 8 9
In this example, the do-while loop will print the numbers from 0 to 9. Loops are a powerful tool that can be used to automate tasks and process data. However, it is important to use them carefully, as they can make code more difficult to understand if they are not used correctly. Here are some additional things to keep in mind about loops: The body of a loop should be indented to make it easier to read. The condition of a loop should be carefully evaluated to make sure that it is always true or always false. The increment or decrement statement should be used to update the loop variable so that the loop terminates eventually.

  📌TAGS

★loop ★looping statement ★control statement ★control in java ★loops in java ★for ★while ★do while ★for each

Tutorials